//
//  AddViewController.m
//  SpiritWalk
//
//  Created by dllo on 16/3/14.
//  Copyright © 2016年 王鑫. All rights reserved.
//

#import "AddViewController.h"

#import "HealthCollectionViewCell.h"

#import "BottomCollectionVIewCell.h"
//codata
#import "CoreDataManager.h"
#import "Health.h"
@interface AddViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property(nonatomic,strong)UICollectionView *titleCollectionView;

@property(nonatomic,copy)NSArray *titleArr;
@property(nonatomic,retain)NSMutableArray* allDataArr;
@property(nonatomic,retain)NSMutableArray* averageDataArr;
@property(nonatomic,strong)UICollectionView *bottomCollectionView;
@property(nonatomic,retain)CoreDataManager * manager;
@property(nonatomic,assign)NSInteger i;

@end

@implementation AddViewController



- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.view.backgroundColor=[UIColor whiteColor];
   // self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"iconfont-cha-copy-2.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ] style:UIBarButtonItemStylePlain target:self action:@selector(backAction:)];
    UIButton * backBut=[[UIButton alloc] initWithFrame:CGRectMake(10, 27, 30, 30)];
    [backBut addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    [backBut setImage:[UIImage imageNamed:@"iconfont-cha-copy-2.png"] forState:UIControlStateNormal];
    [self.view addSubview:backBut];
    [self createDate];
    [self setUpTitleCollectionView];
    
    [self setUpBottomCollectionView];
    
}
-(void)createDate{
     self.allDataArr =[NSMutableArray array];
    self.averageDataArr=[NSMutableArray array];
     self.titleArr=@[@"步数",@"大卡",@"时间",@"公里",@"楼层"];
    self.manager=[CoreDataManager manager];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Health" inManagedObjectContext:self.manager.managedObjectContext];
    [fetchRequest setEntity:entity];
  //  NSPredicate *predicate = [NSPredicate predicateWithFormat:@"date=%@ ", dateString];
    //[fetchRequest setPredicate:predicate];
    NSError *error = nil;
    NSArray *fetchedObjects = [self.manager.managedObjectContext executeFetchRequest:fetchRequest error:&error];
    if (fetchedObjects == nil) {
    }
   
    NSInteger  stepCountAll=0;
    NSInteger  kcalAll=0;
    NSInteger  timeAll=0;
    CGFloat kilAll=0.0;
    NSInteger floorAll=0;
    for (Health * healt in fetchedObjects) {
        stepCountAll+=[healt.stepCount integerValue];
        kcalAll+= [healt.kcal integerValue];
        timeAll+=[healt.time integerValue];
       kilAll+=[healt.kil floatValue];
       floorAll+= [healt.floor integerValue];
        
    }
    [self.allDataArr addObject:[NSString stringWithFormat:@"%ld",stepCountAll ]];
    [self.allDataArr addObject:[NSString stringWithFormat:@"%ld",kcalAll ]];
    [self.allDataArr addObject:[NSString stringWithFormat:@"%ld",timeAll ]];
    [self.allDataArr addObject:[NSString stringWithFormat:@"%.2f",kilAll ]];
    [self.allDataArr addObject:[NSString stringWithFormat:@"%ld",floorAll ]];
    
    [self.averageDataArr addObject:[NSString stringWithFormat:@"%ld",stepCountAll/fetchedObjects.count]];
     [self.averageDataArr addObject:[NSString stringWithFormat:@"%ld",kcalAll/fetchedObjects.count]];
     [self.averageDataArr addObject:[NSString stringWithFormat:@"%ld",timeAll/fetchedObjects.count]];
     [self.averageDataArr addObject:[NSString stringWithFormat:@"%.2f",kilAll/fetchedObjects.count]];
     [self.averageDataArr addObject:[NSString stringWithFormat:@"%ld",floorAll/fetchedObjects.count]];
}
-(void)setUpTitleCollectionView{
    
    UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize=CGSizeMake((WIDTH-50)/5, 64);
    
    flowLayout.minimumInteritemSpacing=0;
    flowLayout.minimumLineSpacing=0;
    flowLayout.scrollDirection=UICollectionViewScrollDirectionHorizontal;
    
    self.titleCollectionView =[[UICollectionView alloc] initWithFrame:CGRectMake(50, 10, WIDTH-50, 64) collectionViewLayout:flowLayout];
    
   [ self.view addSubview: self.titleCollectionView];
    self.titleCollectionView.backgroundColor=[UIColor whiteColor];
    self.titleCollectionView.delegate=self;
    self.titleCollectionView.dataSource=self;
   
    
    [self.titleCollectionView registerClass:[HealthCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([HealthCollectionViewCell class])];
}

-(void)setUpBottomCollectionView{
    
    UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc] init];
    flowLayout.scrollDirection=UICollectionViewScrollDirectionHorizontal;
    flowLayout.minimumInteritemSpacing=0;
    flowLayout.minimumLineSpacing=0;
    
    flowLayout.itemSize=CGSizeMake(WIDTH, HEIGHT-64);
    self.bottomCollectionView=[[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, WIDTH, HEIGHT-64)collectionViewLayout:flowLayout];
    self.bottomCollectionView.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:self.bottomCollectionView];
    self.bottomCollectionView.dataSource=self;
    self.bottomCollectionView.delegate=self;
    self.bottomCollectionView.bounces=NO;
    self.bottomCollectionView.showsVerticalScrollIndicator=NO;
    self.bottomCollectionView.showsHorizontalScrollIndicator=NO;
    self.bottomCollectionView.pagingEnabled=YES;
    
    [self.bottomCollectionView registerClass:[BottomCollectionVIewCell class] forCellWithReuseIdentifier:@"0"];
    [self.bottomCollectionView registerClass:[BottomCollectionVIewCell class] forCellWithReuseIdentifier:@"1"];
    [self.bottomCollectionView registerClass:[BottomCollectionVIewCell class] forCellWithReuseIdentifier:@"2"];
    [self.bottomCollectionView registerClass:[BottomCollectionVIewCell class] forCellWithReuseIdentifier:@"3"];
    [self.bottomCollectionView registerClass:[BottomCollectionVIewCell class] forCellWithReuseIdentifier:@"4"];
    
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    
    return 5;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (collectionView==self.titleCollectionView) {

    HealthCollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([HealthCollectionViewCell class]) forIndexPath:indexPath];
    
    cell.titleLabel.text=self.titleArr[indexPath.item];
    cell.titleLabel.font=[UIFont systemFontOfSize:24];
        [cell.titleLabel setHighlightedTextColor:[UIColor colorWithRed:233/255.0 green:188/255.0 blue:60/255.0 alpha:1]];
    cell.titleLabel.textAlignment=NSTextAlignmentCenter;
        if (indexPath.item==0) {
            cell.selected=YES;
           // cell.titleLabel.textColor=[UIColor colorWithRed:233/255.0 green:188/255.0 blue:60/255.0 alpha:1];
        }else{
            cell.selected=NO;
          //  cell.titleLabel.textColor=[UIColor blackColor];
        }
    cell.opaque=YES;
    return  cell;
    }
    else{
        self.i=indexPath.item;
        
        BottomCollectionVIewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:[NSString stringWithFormat:@"%ld",indexPath.item] forIndexPath:indexPath];
        
        cell.averageTitleLabel.text=[NSString stringWithFormat:@"平均%@",self.titleArr[indexPath.item]];
        cell.tatalTitleLabel.text=[NSString stringWithFormat:@"总%@",self.titleArr[indexPath.item]];
        
        cell.tatalLabel.text=self.allDataArr[indexPath.item];
        cell.averageLabel.text=self.averageDataArr[indexPath.item];
        cell.titleArr=@[@"1",@"2",@"3",@"4",@"5",@"6",@"7"];
        cell.valueArr=@[@"1000",@"266",@"300",@"455",@"1000",@"666",@"777"];
        return cell;
        
    }
    
    return nil;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (collectionView==self.titleCollectionView) {
   
    NSIndexPath *indexpath=[NSIndexPath indexPathForItem:indexPath.item inSection:0];
    
    [self.bottomCollectionView scrollToItemAtIndexPath:indexpath atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
    
    }
    
    
}

-(void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
    
    NSIndexPath *indexpath=[NSIndexPath indexPathForItem:self.i inSection:0];
    NSLog(@"%ld",self.i);
    
    [self.titleCollectionView selectItemAtIndexPath:indexpath animated:YES scrollPosition:UICollectionViewScrollPositionLeft];
    
    
    if (self.i!=0) {
        NSIndexPath *indexpath0=[NSIndexPath indexPathForItem:0 inSection:0];
        HealthCollectionViewCell * cell=(HealthCollectionViewCell *)[self.titleCollectionView cellForItemAtIndexPath:indexpath0];
        cell.selected=NO;
    }
    NSLog(@"000000");
    
}
-(void)backAction:(UIButton*)button{
    [super dismissViewControllerAnimated:YES completion:^{
        
    }];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
